﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Xml;
namespace ContentMigrator
{
    public partial class frmConvertAccess : Form
    {
        public frmConvertAccess()
        {
            InitializeComponent();
        }
        private string ConvertAccessToXML(string dbfile, string tablename, string idfield)
        {
            try
            {
                string connect = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbfile + @";Persist Security Info=False;";
                OleDbConnection cn = new OleDbConnection(connect);
                cn.Open();

                OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + tablename, cn);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet(tablename);
                da.Fill(ds);

                XmlDocument doc = new XmlDocument();
                XmlNode data = doc.CreateNode(XmlNodeType.Element, "Data", string.Empty);
                DataView dv = ds.Tables[0].DefaultView;
                foreach (DataRow myrow in dv.Table.Rows)
                {
                    XmlNode item = doc.CreateNode(XmlNodeType.Element, "Item", string.Empty);
                    foreach (DataColumn col in dv.Table.Columns)
                    {
                        if (col.Caption.ToLower() != idfield.ToLower())
                            item.Attributes.Append(doc.CreateAttribute(col.Caption)).InnerText = myrow[col.Ordinal].ToString();
                    }
                    data.AppendChild(item);
                }
                doc.AppendChild(data);

                string outfile = dbfile.Replace(dbfile.Substring(dbfile.LastIndexOf(".")), ".xml");
                if (File.Exists(outfile))
                    File.Delete(outfile);
                doc.Save(outfile);
                cn.Close();
                return outfile;
            }
            catch (Exception ex)
            {

                throw new Exception("ConvertMDBToExcel: " + ex.Message);
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Access Files|*.mdb;*.accdb";
            fd.ShowDialog();

            lblFile.Text = fd.FileName;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                if (lblFile.Text.Trim() == string.Empty)
                    throw new Exception("Please select an Access database file");

                if (txtTable.Text.Trim() == string.Empty)
                {
                    throw new Exception("Please enter a table name");
                }

                string outfile = ConvertAccessToXML(lblFile.Text, txtTable.Text,txtIDField.Text);
                if (chkOpen.Checked == true)
                {
                    if (StaticMethods.frmLoadMetabase1 != null)
                        StaticMethods.frmLoadMetabase1.Close();

                    StaticMethods.frmLoadMetabase1 = new frmLoadMetabase(outfile);
                    StaticMethods.frmLoadMetabase1.Icon = StaticMethods.frmMainRef.Icon;
                    StaticMethods.frmLoadMetabase1.MdiParent = StaticMethods.frmMainRef;
                    StaticMethods.frmLoadMetabase1.Show();
                    this.Close();
                } else
                    MessageBox.Show("Conversion was successful.", "CONVERTED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
